﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Webdiyer.WebControls.Mvc;

namespace MinistryStandardPlatformWeb
{
    public static class HtmlHelper_Mypager
    {
        public static MvcHtmlString MyPager<T>(this HtmlHelper htmlHelper, PagedList<T> pagedList, string pagerDivId,string style)
        {
            if (pagedList.TotalPageCount <= 1)
            {
                return MvcHtmlString.Create("");
            }

            TagBuilder tag = new TagBuilder("div");
            tag.Attributes.Add("id", pagerDivId);
            tag.Attributes.Add("style", style);

            string innerhtml = string.Empty;
            TagBuilder tag1 = new TagBuilder("a");
            tag1.SetInnerText("首页");
            tag1.Attributes.Add("value","1");
            if (pagedList.CurrentPageIndex == 1)
            {
                tag1.Attributes.Add("disabled", "disabled");
                tag1.Attributes.Add("style", "color:#bbb");
            }
            innerhtml += MvcHtmlString.Create(tag1.ToString(TagRenderMode.Normal));
            innerhtml += "&nbsp;&nbsp;";

            tag1 = new TagBuilder("a");
            tag1.SetInnerText("上一页");
            if (pagedList.CurrentPageIndex == 1)
            {
                tag1.Attributes.Add("disabled", "disabled");
                tag1.Attributes.Add("style", "color:#bbb");
                tag1.Attributes.Add("value","1");
            }
            else
            {
                tag1.Attributes.Add("value",(pagedList.CurrentPageIndex-1).ToString());
            }
            innerhtml += MvcHtmlString.Create(tag1.ToString(TagRenderMode.Normal));
            innerhtml += "&nbsp;&nbsp;";

            if (pagedList.TotalPageCount <= 15)
            {
                for (int i = 1; i <= pagedList.TotalPageCount; i++)
                {
                    tag1 = new TagBuilder("a");
                    tag1.SetInnerText(i.ToString());
                    tag1.Attributes.Add("value", i.ToString());
                    if (i == pagedList.CurrentPageIndex)
                    {
                        tag1.Attributes.Add("style", "color: #ff0000;text-decoration: underline;");
                    }
                    innerhtml += MvcHtmlString.Create(tag1.ToString(TagRenderMode.Normal));
                    innerhtml += "&nbsp;&nbsp;";
                }
            }
            else //多于15页只显示15页的页码
            {
                int start = 1 >= pagedList.CurrentPageIndex - 7 ? 1 : pagedList.CurrentPageIndex - 7;
                if (pagedList.CurrentPageIndex + 7 > pagedList.TotalPageCount)
                {
                    start -= (pagedList.CurrentPageIndex + 7 - pagedList.TotalPageCount);
                }
                if (start > 1) //显示省略号
                {
                    tag1 = new TagBuilder("label");
                    tag1.SetInnerText("<<");
                    tag1.Attributes.Add("disabled", "disabled");
                    tag1.Attributes.Add("style", "color:#bbb");
                    innerhtml += MvcHtmlString.Create(tag1.ToString(TagRenderMode.Normal));
                    innerhtml += "&nbsp;&nbsp;";
                }

                int end=start;
                for (int i = 0; i < 15 && end <= pagedList.TotalPageCount; i++)
                {
                    end = i + start;
                    tag1 = new TagBuilder("a");
                    tag1.SetInnerText(end.ToString());
                    tag1.Attributes.Add("value", end.ToString());
                    if (end == pagedList.CurrentPageIndex)
                    {
                        tag1.Attributes.Add("style", "color: #ff0000;text-decoration: underline;");
                    }
                    innerhtml += MvcHtmlString.Create(tag1.ToString(TagRenderMode.Normal));
                    innerhtml += "&nbsp;&nbsp;";
                }

                if (end < pagedList.TotalPageCount) //显示省略号
                {
                    tag1 = new TagBuilder("label");
                    tag1.SetInnerText(">>");
                    tag1.Attributes.Add("disabled", "disabled");
                    tag1.Attributes.Add("style", "color:#bbb");
                    innerhtml += MvcHtmlString.Create(tag1.ToString(TagRenderMode.Normal));
                    innerhtml += "&nbsp;&nbsp;";
                }

            }

            tag1 = new TagBuilder("a");
            tag1.SetInnerText("下一页");
            if (pagedList.CurrentPageIndex == pagedList.TotalPageCount)
            {
                tag1.Attributes.Add("disabled", "disabled");
                tag1.Attributes.Add("style", "color:#bbb");
                tag1.Attributes.Add("value", pagedList.TotalPageCount.ToString());
            }
            else
            {
                tag1.Attributes.Add("value", (pagedList.CurrentPageIndex + 1).ToString());
            }
            innerhtml += MvcHtmlString.Create(tag1.ToString(TagRenderMode.Normal));
            innerhtml += "&nbsp;&nbsp;";

            tag1 = new TagBuilder("a");
            tag1.SetInnerText("尾页");
            tag1.Attributes.Add("value", pagedList.TotalPageCount.ToString());
            if (pagedList.CurrentPageIndex == pagedList.TotalPageCount)
            {
                tag1.Attributes.Add("disabled", "disabled");
                tag1.Attributes.Add("style", "color:#bbb;cursor:default");
            }
            innerhtml += MvcHtmlString.Create(tag1.ToString(TagRenderMode.Normal));
            innerhtml += "&nbsp;&nbsp;";

            //下拉框
            TagBuilder tag2 = new TagBuilder("select");
            tag2.Attributes.Add("style", "overflow-y:auto;");
            string innerhtml1 = string.Empty;
            TagBuilder tag3;
            for (int i = 1; i <= pagedList.TotalPageCount; i++)
            {
                tag3 = new TagBuilder("option");
                tag3.SetInnerText(i.ToString());
                tag3.Attributes.Add("value", i.ToString());
                if (i == pagedList.CurrentPageIndex)
                {
                    tag3.Attributes.Add("selected", "selected");
                }
                innerhtml1 += MvcHtmlString.Create(tag3.ToString(TagRenderMode.Normal));
            }
            tag2.InnerHtml = innerhtml1;

            innerhtml += MvcHtmlString.Create(tag2.ToString(TagRenderMode.Normal));
            tag.InnerHtml = innerhtml;

            return MvcHtmlString.Create(tag.ToString(TagRenderMode.Normal));

        }
    }
}